package example.demo.config;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RabbitMQSend {
    @Autowired
    RabbitTemplate rabbitTemplate;
    private final String exchangeA = "rao.exchange.aaa";
    private final String exchangeB = "rao.exchange.bbb";
    private final String exchangeC = "rao.exchange.ccc";
    private final String routeA = "rao.route.aaa";
    private final String routeB = "rao.route.bbb";
    private final String routeC = "rao.route.ccc";


    /**
     * todo 消息的路由
     */
    public String sendMessage(String message) {
        if (message.startsWith("A")) {
            return sendMessageToA(message);
        } else if (message.startsWith("B")) {
            return sendMessageToB(message);
        } else {
            return sendMessageToC(message);
        }
    }

    private String sendMessageToA(String message) {
        rabbitTemplate.convertAndSend(exchangeA, routeA, message);
        return "succeed-AAA";
    }

    private String sendMessageToB(String message) {
        rabbitTemplate.convertAndSend(exchangeB, routeB, message);
        return "succeed-BBB";
    }

    private String sendMessageToC(String message) {
        rabbitTemplate.convertAndSend(exchangeC, routeC, message);
        return "succeed-CCC";
    }
}
